package cn.zzf.controller;

import cn.zzf.core.*;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ${DESCRIPTION}
 *
 * @author GaoFeng2017
 * @date 2018-02-13 09:49:20
 **/

@javax.servlet.annotation.WebServlet(name = "TeacherLessonServlet", urlPatterns = {"/TeacherLessonServlet"})

public class TeacherLessonServlet extends javax.servlet.http.HttpServlet {

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String sessionId = (String) request.getSession().getAttribute("sessionId");
        String captcha = request.getParameter("captcha");
        String oldId = request.getParameter("id");
        Logger logger = LogManager.getLogger(this.getClass());

        int id = Integer.parseInt(oldId);
        String idStr = null;
        try {

            ContentCrawer crawer = new ContentCrawer.ContentCrawerBuilder()
                    .setCookie(createCookie(sessionId))
                    .setEncode("utf-8")
                    .setFilterRegex("(<br>)|(；)")
                    .setReferer("http://218.65.5.214:2001/jwweb/ZNPK/TeacherKBFB.aspx")
                    .setUrl("http://218.65.5.214:2001/jwweb/ZNPK/TeacherKBFB_rpt.aspx")
                    .build();
            do {
                idStr = "WP" + CrawerUtils.getFillZero(5 - CrawerUtils.getNumberLength(id)) + id;
//                idStr = CrawerUtils.getFillZero(7 - CrawerUtils.getNumberLength(id)) + id;
                Map<String,String> map = createParamMap(idStr,captcha);
                crawer.setParamMap(map);

                ClassTableHandler handler = new ClassTableHandler();
                boolean b = crawer.execute(handler,"部门",idStr);
//                boolean b = crawer.execute(new LinkHandler(), "学分", idStr);
                if (b) {
                    logger.info("教师id" + idStr + "已记录");
                } /*else {
                    logger.info("教师id" + idStr + "未匹配");
                }*/

                id++;
            } while(true);

        } catch (CaptchaInputException e) {
            request.setAttribute("idStr",idStr);
            request.setAttribute("id",id);
            request.setAttribute("sid",sessionId);
            request.getRequestDispatcher("/get_class_table.jsp").forward(request,response);
            return;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public BasicClientCookie createCookie(String sessionId) {
        BasicClientCookie cookie = new BasicClientCookie("ASP.NET_SessionId", sessionId);
        cookie.setVersion(0);
        cookie.setDomain("218.65.5.214");
        cookie.setPath("/");
        return cookie;
    }

    public Map<String, String> createParamMap(String id, String captcha) {
        Map<String,String> map = new HashMap<>(4);
        map.put("Sel_XNXQ", "20171");
        map.put("type", "1");
        map.put("txt_yzm", captcha);
        map.put("Sel_JS", id);
        return map;
    }
}
